package jqgridjavawrapper;

/**
 * @author Isuru Madushanka Weerarathna
 * @contact uisurumadushanka89@gmail.com
 */
public class JQGridColumn implements JQPropertyMaker {

    // Defines the alignment of the cell in the Body layer, not in header cell. Possible values: left, center, right.
    private String alignment = null;

    /*This option allow to add classes to the column. If more than one class will be used a space should be set.
     * By example classes:'class1 class2' will set a class1 and class2 to every cell on that column.
     * In the grid css there is a predefined class ui-ellipsis which allow to attach ellipsis to a particular row.
     * Also this will work in FireFox too. */
    private String cssClasses = null;

    /* Cell editable options */
    private boolean editable = false;
    private String editType = "text";
    private String editOptions = null;
    private JQGridColumnEditRules editRules = null;

    /* Cell formatter options */
    private JQGridColumnFormatter formatter = null;
    private String firstSortOrder = null;

    /*
     * If set to true this option does not allow recalculation of the width of the column if shrinkToFit
     * option is set to true. Also the width does not change if a setGridWidth method is used to change
     * the grid width.
     */
    private boolean fixed = false;

    /*
     * Defines if this column is hidden at initialization.
     */
    private boolean hidden = false;

    /*
     * Set the index name when sorting. Passed as sidx parameter.
     */
    private String indexName = null;

    /*Set the unique name in the grid for the column. This property is required.
     * As well as other words used as property/event names, the reserved words
     * (which cannot be used for names) include subgrid, cb and rn. */
    private String name = null;
    private boolean searchable = true;
    private String searchType = "text";
    private String searchOptions = null;

    /* Defines if the column can be re sized */
    private boolean resizable = true;

    /* Defines is this can be sorted. */
    private boolean sortable = true;

    /* display caption of column */
    private String caption = "";
    private boolean hideColumnInShowHideDialog = false;
    private String jsonIDforThisColumn = null;
    private boolean key = false;
    private boolean showInViewForm = true;
    /**
     * Used when datatype is local. Defines the type of the column for appropriate sorting.Possible values:
     * int/integer - for sorting integer
     * float/number/currency - for sorting decimal numbers
     * date - for sorting date
     * text - for text sorting
     * function - defines a custom function for sorting. To this function we pass the value to be sorted and it should return a value too.
     * */
    private String sortType = null;

    /* Set the initial width of the column, in pixels. This value currently can not be set as percentage */
    private int width = 150;
    private String unformatFunctionName = null;

    public JQGridColumn(String colName) {
        this.name = colName;
    }

    private String convertToJSon(String attrib, String value) {
        return attrib + ": '" + value + "'";
    }

    private String convertToJSon(String attrib, int value) {
        return attrib + ": " + value + "";
    }

    private String convertToJSon(String attrib, boolean value) {
        return attrib + ": " + (value ? "true" : "false") + "";
    }

    public String getAlignment() {
        return alignment;
    }

    public void setAlignment(String alignment) {
        this.alignment = alignment;
    }

    public String getCssClasses() {
        return cssClasses;
    }

    public void setCssClasses(String cssClasses) {
        this.cssClasses = cssClasses;
    }

    public boolean isEditable() {
        return editable;
    }

    public void setEditable(boolean editable) {
        this.editable = editable;
    }

    public String getEditType() {
        return editType;
    }

    public void setEditType(String editType) {
        this.editType = editType;
    }

    public String getEditOptions() {
        return editOptions;
    }

    public void setEditOptions(String editOptions) {
        this.editOptions = editOptions;
    }

    public JQGridColumnEditRules getEditRules() {
        return editRules;
    }

    public void setEditRules(JQGridColumnEditRules editRules) {
        this.editRules = editRules;
    }

    public JQGridColumnFormatter getFormatter() {
        return formatter;
    }

    public void setFormatter(JQGridColumnFormatter formatter) {
        this.formatter = formatter;
    }

    public String getFirstSortOrder() {
        return firstSortOrder;
    }

    public void setFirstSortOrder(String firstSortOrder) {
        this.firstSortOrder = firstSortOrder;
    }

    public boolean isFixed() {
        return fixed;
    }

    public void setFixed(boolean fixed) {
        this.fixed = fixed;
    }

    public boolean isHidden() {
        return hidden;
    }

    public void setHidden(boolean hidden) {
        this.hidden = hidden;
    }

    public String getIndexName() {
        return indexName;
    }

    public void setIndexName(String indexName) {
        this.indexName = indexName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSearchable() {
        return searchable;
    }

    public void setSearchable(boolean searchable) {
        this.searchable = searchable;
    }

    public String getSearchType() {
        return searchType;
    }

    public void setSearchType(String searchType) {
        this.searchType = searchType;
    }

    public String getSearchOptions() {
        return searchOptions;
    }

    public void setSearchOptions(String searchOptions) {
        this.searchOptions = searchOptions;
    }

    public boolean isResizable() {
        return resizable;
    }

    public void setResizable(boolean resizable) {
        this.resizable = resizable;
    }

    public boolean isSortable() {
        return sortable;
    }

    public void setSortable(boolean sortable) {
        this.sortable = sortable;
    }

    public String getCaption() {
        return caption;
    }

    public void setCaption(String caption) {
        this.caption = caption;
    }

    public boolean isHideColumnInShowHideDialog() {
        return hideColumnInShowHideDialog;
    }

    public void setHideColumnInShowHideDialog(boolean hideColumnInShowHideDialog) {
        this.hideColumnInShowHideDialog = hideColumnInShowHideDialog;
    }

    public String getJsonIDforThisColumn() {
        return jsonIDforThisColumn;
    }

    public void setJsonIDforThisColumn(String jsonIDforThisColumn) {
        this.jsonIDforThisColumn = jsonIDforThisColumn;
    }

    public boolean isKey() {
        return key;
    }

    public void setKey(boolean key) {
        this.key = key;
    }

    public boolean isShowInViewForm() {
        return showInViewForm;
    }

    public void setShowInViewForm(boolean showInViewForm) {
        this.showInViewForm = showInViewForm;
    }

    public String getSortType() {
        return sortType;
    }

    public void setSortType(String sortType) {
        this.sortType = sortType;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public String getUnformatFunctionName() {
        return unformatFunctionName;
    }

    public void setUnformatFunctionName(String unformatFunctionName) {
        this.unformatFunctionName = unformatFunctionName;
    }

    public StringBuilder generateProperties() {
        StringBuilder sb = new StringBuilder();

        sb.append("{");
        sb.append(convertToJSon("name", this.name));
        if (this.caption != null) {
            sb.append(",").append(convertToJSon("label", this.caption));
        }
        if (this.alignment != null) {
            sb.append(",");
            sb.append(convertToJSon("align", this.alignment));
        }
        if (this.cssClasses != null) {
            sb.append(",");
            sb.append(convertToJSon("classes", this.cssClasses));
        }
        if (this.jsonIDforThisColumn != null) {
            sb.append(",");
            sb.append("jsonmap: '").append(this.jsonIDforThisColumn).append("'");
        }
        if (this.fixed == true) {
            sb.append(",");
            sb.append(convertToJSon("fixed", this.fixed));
        }
        if (this.hidden == true) {
            sb.append(",");
            sb.append(convertToJSon("hidden", this.hidden));
        }
        if (this.indexName != null) {
            sb.append(",");
            sb.append(convertToJSon("index", this.indexName));
        }
        if (this.key) {
            sb.append(",key:true");
        }
        if (this.resizable == false) {
            sb.append(",").append(convertToJSon("resizable", this.resizable));
        }
        if (this.sortable == false) {
            sb.append(",");
            sb.append(convertToJSon("sortable", this.sortable));
        }
        if (this.sortType != null) {
            sb.append(",");
            sb.append(convertToJSon("sorttype", this.sortType));
        }
        if (this.width != 150) {
            sb.append(",");
            sb.append(convertToJSon("width", this.width));
        }
        if (this.firstSortOrder != null) {
            sb.append(",").append(convertToJSon("firstsortorder", this.firstSortOrder));
        }
        if (this.hideColumnInShowHideDialog) {
            sb.append(",").append("hidedlg:true");
        }

        if (this.editable) {
            sb.append(", editable: true, edittype: '");
            sb.append(this.editType).append("'");
            if (this.editOptions != null) {
                sb.append(", editoptions: ");
                if (this.editOptions.startsWith("{")) {
                    sb.append(this.editOptions);
                } else {
                    sb.append("{").append(this.editOptions).append("}");
                }
            }
            if (this.editRules != null) {
                sb.append(", editrules: ");
                sb.append(this.editRules.generateProperties().toString());
            }
        }
        if (this.formatter != null) {
            sb.append(", ").append(this.formatter.generateProperties().toString());
        }

        if (this.searchable) {
            sb.append(", search: true, stype: '").append(this.searchType).append("'");
            if (this.searchOptions != null) {
                sb.append(", searchoptions: ");
                if (this.searchOptions.startsWith("{")) {
                    sb.append(this.searchOptions);
                } else {
                    sb.append("{").append(this.searchOptions).append("}");
                }
            }

        }

        if (this.unformatFunctionName != null) {
            sb.append(", unformat:").append(this.unformatFunctionName);
        }
        if (this.showInViewForm == false) {
            sb.append(", viewable:true");
        }

        sb.append("}");
        return sb;
    }
}
